/********************************** (C) COPYRIGHT *******************************
 * File Name          : FLASH_IC.C
 * Author             : GJ
 * Version            : V2.00
 * Date               : 2018/07/10
 * Description        : FLASH Read
 *******************************************************************************/

/*******************************************************************************
 * configure information:
 * Name			Address			Data
 * x_resolution  0xc000 0xc002
 * y_resolution  0xc004 0xc006
 * XMirror		0xc000 bit7
 * YMirror	    0xc004 bit7
 *******************************************************************************/

/* 头文件包含 */
#include "Flash.h"
#include "Device.h"

uint8_t GFlashData[FLASH_DATA_LEN];
/* Get flash info */
uint8_t GetConfigInfo(void)
{
	uint8_t i;
	ROM_ADDR_H = DATA_FLASH_ADDR >> 8;

	for (i = 0; i < FLASH_DATA_LEN; i++)
	{
		ROM_ADDR_L = i * 2;
		ROM_CTRL = ROM_CMD_READ;
		//     if ( ROM_STATUS & bROM_CMD_ERR ) return( 0xFF );                        // unknown command
		*(GFlashData + i) = ROM_DATA_L;
	}
	#if DE_PRINTF
		printf("data flash read: ");
		for (i = 0; i < FLASH_DATA_LEN; i++)
		{
			printf("%02x\t", (uint16_t)GFlashData[i]);
		}
		printf("\n");
	#endif

	if ((GFlashData[0] == 0xFF) && (GFlashData[1] == 0xFF)) // 空的FLASH
	{
		GCtrlTouchPadInfo.XMirror = 0;
	}
	else
	{
		if ((GFlashData[0] & 0x80) != 0)
		{
			GCtrlTouchPadInfo.XMirror = 1;
			GFlashData[0] &= 0x7F;
		}
		else
		{
			GCtrlTouchPadInfo.XMirror = 0;
		}
	}

	if ((GFlashData[2] == 0xFF) && (GFlashData[3] == 0xFF))
	{
		GCtrlTouchPadInfo.YMirror = 0;
	}
	else
	{
		if ((GFlashData[2] & 0x80) != 0)
		{
			GCtrlTouchPadInfo.YMirror = 1;
			GFlashData[2] &= 0x7F;
		}
		else
		{
			GCtrlTouchPadInfo.YMirror = 0;
		}
	}

	// 在Flash_Data[5]为0x0a（10）的时候使用flash配置值,否则使用从屏读出的值
	if ((GFlashData[5] == 10) || ((GCtrlTouchPadInfo.XPosMax == 0) && (GCtrlTouchPadInfo.YPosMax == 0)))
	{
		GCtrlTouchPadInfo.XPosMax = ((uint16_t)(GFlashData[0]) << 8) + GFlashData[1];
		GCtrlTouchPadInfo.YPosMax = ((uint16_t)(GFlashData[2]) << 8) + GFlashData[3];
	}

	GCtrlTouchPadInfo.SwapXY = (GFlashData[4] == 0x01) ? 1 : 0;

	return 1;
}
